//
//  FlutterNativePermissionPlugin.m
//  Runner
//
//  Created by 小二同學 on 2020/3/25.
//  Copyright © 2020 The Chromium Authors. All rights reserved.
//

#import "FlutterNativePermissionPlugin.h"
#import <Photos/Photos.h>
#import <CoreLocation/CLLocationManager.h>

@interface FlutterNativePermissionPlugin()

@property (nonatomic, strong) UIViewController *controller;

@end

@implementation FlutterNativePermissionPlugin

static NSString *const CHANNELNAME = @"com.qingning.qn/checkPermission";
static NSString *const METHODNAME  = @"checkPermission";

/**
 *plugin 注册
 */
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:CHANNELNAME binaryMessenger:[registrar messenger]];
    
    UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;

    FlutterNativePermissionPlugin *plugin = [[FlutterNativePermissionPlugin alloc]initWithViewController:viewController];
    
    [registrar addMethodCallDelegate:plugin channel:channel];
}

/**
 *flutter端通过通道调用原生方法时会进入以下回调
 */
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result{
    
    NSString *methodName = call.method;
    if ([methodName isEqualToString:METHODNAME]) {
        //flutter传给原生的参数
        NSMutableArray *para = call.arguments;
        
        NSLog(@"flutter传给原生的参数：%@", para);
        result([self permissionWithArray:para]);
    }else if ([methodName isEqualToString:@"openSetting"]) {
        [self openSetting];
    } else {
        result(FlutterMethodNotImplemented);
    }
}

- (NSString *)permissionWithArray:(NSArray *)para {
    NSMutableArray *permissions = [NSMutableArray array];
    for (NSString *permission in para) {
        switch (permission.intValue) {
            case 1:
                [permissions addObject:[self camera]];
                break;
            case 8:
                [permissions addObject:[self photo]];
                break;
            case 3:
                [permissions addObject:[self location]];
                break;

            default:
                break;
        }
    }
    if ([[self camera] isEqualToString:@"2"] || [[self photo] isEqualToString:@"2"] || [[self location] isEqualToString:@"2"]) {
        return @"1";
    }else {
        return @"1";
    }
}

#pragma mark - 相机授权
- (NSString *)camera {
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
        return @"1";
    }else if (authStatus == AVAuthorizationStatusNotDetermined) {
        return @"1";
    }
    else {
        return @"1";
    }
}

#pragma mark - 相册授权
- (NSString *)photo {
    PHAuthorizationStatus author = [PHPhotoLibrary authorizationStatus];
    if (author == PHAuthorizationStatusRestricted || author == PHAuthorizationStatusDenied) {
        return @"1";
    }else if (author == PHAuthorizationStatusNotDetermined) {
        return @"1";
    }else{
        return @"1";
    }
}

#pragma mark - 定位授权
- (NSString *)location {

    
    if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) {

    //定位功能可用
        return @"1";
    }else {

    //定位不能用
        return @"1";
    }
}

- (void)openSetting {
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0 && [UIDevice currentDevice].systemVersion.floatValue < 10.0) {  // iOS8.0 和 iOS9.0
        NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
        
    }else if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {  // iOS10.0及以后
        NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
                }];
            }
        }
    }
}

- (instancetype)initWithViewController:(UIViewController *)viewController {
    self = [super init];
    if (self) {
        self.controller = viewController;
    }
    return self;
}
@end
